<script>
export default {
  name: 'institutionSelect',
  emits: ['nodeClick'],
  props: {
    placeholder: {
      type: String
    }
  },
  data() {
    return {
      isFocus: false,
      treeList: []
    }
  },

  methods: {
    /*
    * 获取树状机构信息的回调函数
    *  */
    async fetchInstitutionTree() {
      try {
        this.treeList = await this.$store.dispatch('organizationStore/fetchInstitutionTreeAction')
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    * 关闭下拉框的回调函数
    *  */
    handleClose() {
      this.isFocus=false
    },
    /*
    * 节点被点击的回调函数
    *  */
    nodeClick(node) {
      this.$emit('nodeClick', { id: node.id, name: node.name })
      setTimeout(() => {
        this.isFocus = false
      }, 1000)
    },
    handleFocus() {
      this.isFocus = true
    }

  },
  mounted() {
    this.fetchInstitutionTree()
  }
}
</script>

<template>
  <div class="institution-select">
    <div class="select">
      <input class="input" :placeholder="placeholder" @focus="handleFocus"></input>
      <i :class="isFocus? 'el-icon-arrow-up':'el-icon-arrow-down'" @click="handleClose"></i>
    </div>
    <div class="tree" v-show="isFocus">
      <el-scrollbar class="scroll-bar">
        <el-tree class="filter-tree customer-tree-box" icon-class="el-icon-folder-opened" highlight-current ref="treeRef" node-key="id" :data="treeList" default-expand-all @node-click="nodeClick">
          <template #default="{node,data}">
            <span>{{ data.name }}</span>
          </template>
        </el-tree>
      </el-scrollbar>
    </div>
  </div>
</template>

<style scoped lang="scss">
.institution-select {
  width: 100%;
  height: 40px;
  position: relative;

  .select {
    width: 100%;
    height: 100%;

    i{
      cursor: pointer;
    }

    .input {
      width: 264px;
      height: 100%;
      color: #000;
      border: 1px solid #ddd;
      border-radius: 4px;
      padding-left: 10px;
    }

    ::-webkit-input-placeholder {
      color: #ddd;
    }

    i {
      position: relative;
      right: 25px;
      color: #C0C4CC;
      font-size: 14px;
    }

  }

  .tree {
    position: fixed;
    width: 264px;
    height: 360px;
    background-color: #ffffff;
    border: 1px solid #ddd;
    z-index: 999999;
    padding: 10px 5px;

    .scroll-bar {
      height: 350px;
    }
  }
}
</style>
